package zjut.raymond.collectorplus.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import zjut.raymond.collectorplus.R;

public class CustomRadioButton extends Selector {
    private TextView tvSelectTitle, tvSelectInfo;
    private ImageView imgIcon;
    private ConstraintLayout constraintLayout;
    private String title;
    private String info;
    private int resId;
    private ValueAnimator valueAnimator;

    public CustomRadioButton(@NonNull Context context) {
        super(context);
    }

    public CustomRadioButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRadioButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onObtainAttrs(TypedArray typedArray) {
        //解析自定义属性
        title = typedArray.getString(R.styleable.Selector_title);
        info = typedArray.getString(R.styleable.Selector_info);
        resId = typedArray.getResourceId(R.styleable.Selector_img_src, 0);
    }

    @Override
    protected View onCreateView() {
        View view = LayoutInflater.from(this.getContext()).inflate(R.layout.custom_radiobutton, null);
        constraintLayout = view.findViewById(R.id.cl_select);
        tvSelectTitle = view.findViewById(R.id.tv_select_title);
        tvSelectInfo = view.findViewById(R.id.tv_select_info);
        imgIcon = view.findViewById(R.id.img_expend);
        if (tvSelectTitle != null) {
            tvSelectTitle.setText(title);
        }
        if (tvSelectInfo != null) {
            tvSelectInfo.setText(info);
        }
        if (imgIcon != null) {
            imgIcon.setImageResource(resId);
        }
        return view;
    }

    @Override
    protected void onSwitchSelected(boolean selected) {
        if (selected) {
            imgIcon.setImageResource(R.drawable.ic_expand_less);
            constraintLayout.setBackground(getContext().getResources().getDrawable(R.drawable.ic_select_background));
        } else {
            imgIcon.setImageResource(R.drawable.ic_expand_more);
            constraintLayout.setBackground(getContext().getResources().getDrawable(R.drawable.ic_border_line));
        }
    }

    public void setInfo(String info) {
        if (!TextUtils.isEmpty(info)) {
            tvSelectInfo.setText(info);
        }
    }

    public void setIconDrawable(int resId) {
        imgIcon.setImageResource(resId);
    }

//    private void playUnselectedAnimation() {
//        imgIcon.setImageResource(R.drawable.ic_expand_less);
//        if (constraintLayout == null) return;
//        if (valueAnimator != null) {
//            valueAnimator.reverse();
//        }
//    }

    private void playSelectedAnimation() {
//        imgIcon.setImageResource(R.drawable.ic_expand_more);
//        if (constraintLayout == null) return;
//        valueAnimator = ValueAnimator.ofInt(0, 255);
//        valueAnimator.setDuration(400);
//        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                constraintLayout.setAlpha((int)animation.getAnimatedValue());
//            }
//        });
//        valueAnimator.start();
    }
}
